// pages/discounts/discounts.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    istrue: true,
    couponsList: [],
    couponsLists: [],
    page: 1,
    page_size: 5, 
    no_data: 0,
    have: true,
    haves: true,
    onearth: false, // 是否已经到达底部

  },
  nav() {
    let istrue = this.data.istrue
    this.setData({
      istrue: !istrue,
      have: true,
      haves: true,
      page: 1,
      page_size: 5,
      no_data: 0,
      onearth: false,
      couponsList: [],
      couponsLists: [],
    })
    if (this.data.istrue){
    this.getCoupon()
    }else{
    this.getCoupons()
    }
  },
  //单店优惠券
  getCoupon: function() {
    let that = this;
    let page_size = this.data.page_size,
      no_data = this.data.no_data,
      page = this.data.page;
    if (no_data == 0) {
      console.log(1)
      wh.Post("/api/mall.coupon/lists", {
        page: page,
        page_num: page_size,
        source: 1
      }, function(res) {
        console.log('单店优惠券', res)
        let shop = that.data.couponsList.concat(res),
          count = res.length;
        if (page_size > count) {
          that.data.no_data = 1;
          if (shop.length > 3) {
            that.setData({
              onearth: true
            })
          }
        }
        that.setData({
          couponsList: res
        });
        if (that.data.couponsList.length == 0) {
          that.setData({
            have: false,
            haves: true,
          });
        } else {
          that.setData({
            have: true,
            haves: true,
          });
        }
      })
    }
  },
  //系统优惠券
  getCoupons: function() {
    let that = this;
    let page_size = this.data.page_size,
      no_data = this.data.no_data,
      page = this.data.page;
    if (no_data == 0) {
      console.log(1)
      wh.Post("/api/mall.coupon/lists", {
        page: page,
        page_num: page_size,
        source: 2
      }, function(res) {
        console.log('系统优惠券', res)
        let shop = that.data.couponsLists.concat(res),
          count = res.length;
        if (page_size > count) {
          that.data.no_data = 1;
          if (shop.length > 3) {
            that.setData({
              onearth: true
            })
          }
        }
        that.setData({
          couponsLists: shop
        });
        if (that.data.couponsLists.length == 0) {
          that.setData({
            have: true,
            haves: false,
          });
        } else {
          that.setData({
            have: true,
            haves: true,
          });
        }
      })
    }
  },
  //领取优惠券
  linquCoupon: function (e) {
    let that = this;
    let coupon_id = e.currentTarget.dataset.id;
    let idx = e.currentTarget.dataset.idx;
    let couponsList = that.data.couponsList;
    let couponsLists = that.data.couponsLists;
    wh.Post('/api/mall.coupon/receive', { coupon_id: coupon_id }, function (res) {
      couponsList[idx].is_receive = 1
      couponsLists[idx].is_receive = 1
      that.setData({
        couponsList: couponsList,
        couponsLists: couponsLists
      })
      wx.showToast({ title: '领取成功', icon: 'none' });
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getCoupon()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      no_data: 0,
      page: 1,
      page_size: 5,
      onearth: false,
      couponsLists: [],
      couponsList: [],
    });
    this.getCoupon()
    this.getCoupons()
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let no_data = this.data.no_data,
      page = this.data.page,
      page_size = this.data.page_size;
    if (no_data == 0) {
      this.data.page = page + 1;
      this.getCoupon()
      this.getCoupons()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})